﻿using DevComponents.DotNetBar.SuperGrid;
using SDATA;
using SDLL;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SABI
{
    public partial class frm0099 : Form
    {

        string sql = "";
        string _WareHouseID;

        public frm0099()
        {
            InitializeComponent();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            (Application.OpenForms["frmHOME"] as frmHOME).CloseTab(this);
        }

        private void frm0099_Shown(object sender, EventArgs e)
        {
            loadDATA_Grid1();
        }

        public void loadDATA_Grid1()
        {
            sql = string.Format("Select * from tb0055 WHERE DivisionID='{0}' Order by WareHouseID", FRM.DivisionID) ;
            GridDB1.PrimaryGrid.DataSource = FRM.GetDataTable(sql);
            for(int i=0;i<GridDB1.PrimaryGrid.Columns.Count; i++)
                GridDB1.PrimaryGrid.Columns[i].FilterMatchType = DevComponents.DotNetBar.SuperGrid.FilterMatchType.Wildcards;
        }

        private void GridDB1_SelectionChanged(object sender, DevComponents.DotNetBar.SuperGrid.GridEventArgs e)
        {
            GridRow gr = (GridRow)e.GridPanel.ActiveRow;
            _WareHouseID = gr.Cells["clWareHouseID"].Value.ToString();
        }

        void nAdd()
        {
            Form frm = new frm0107();
            frm.ShowDialog();
        }

        void nEdit()
        {
            Form frm = new frm0107(false, false, _WareHouseID);
            frm.ShowDialog();
        }

        void nDelete()
        {
            if (GridDB1.PrimaryGrid.GetSelectedRows().Count < 1)
            {
                SDLL.DLL02.Alert("Bạn phải chọn dòng cần xóa!");
                return;
            }
            if (!SDLL.DLL02.Confirm("Bạn có chắc muốn xóa?", "Xác nhận")) { return; }

            sql = "pr0071 'tb0055','" + _WareHouseID + "'";
            DataTable dtT =  FRM.GetDataTable(sql);
            if (dtT.Rows.Count == 0) 
            {
                DLL02.Info("Lỗi truy vấn dữ liệu.  Xóa thất bại!");
                return;
            }
            if(dtT.Rows[0][0].ToString()=="1")
            {
                DLL02.Info("Kho hàng " + _WareHouseID + " đang sử dụng. Xóa thất bại!");
                return;
            }
            sql = "DELETE FROM tb0055 WHERE WareHouseID ='" + _WareHouseID + "' AND DivisionID= '" + FRM.DivisionID + "'";
            if(DATA.ExecuteNonQuery(sql, FRM.ConnectString,false))
            {
                //bỏ qua chạy pr0092 (do phân quyền kiểu mới)
                DLL02.Info("Xóa kho thành công!");
                nRefresh();
                return;
            }
            else
            {
                DLL02.Alert("Xóa kho thất bại!");
                return;
            }

        }

        void nView()
        {
            Form frm = new frm0107(false, true, _WareHouseID);
            frm.ShowDialog();
        }

        void nInfo()
        {
            string tn = "tb0055";
            string ws = string.Format("WHERE WareHouseID='{0}'", _WareHouseID);
            Form frm = new frm0067(tn, ws);
            frm.ShowDialog();
        }

        void nRefresh()
        {
            loadDATA_Grid1();
        }

        private void GridDB1_RowDoubleClick(object sender, GridRowDoubleClickEventArgs e)
        {
            nEdit();
        }

        private void frm0099_Load(object sender, EventArgs e)
        {

        }

        private void mAdd_Click(object sender, EventArgs e)
        {
            nAdd();
        }

        private void mEdit_Click(object sender, EventArgs e)
        {
            nEdit();
        }

        private void mDelete_Click(object sender, EventArgs e)
        {
            nDelete();
        }

        private void mView_Click(object sender, EventArgs e)
        {
            nView();
        }

        private void mRefresh_Click(object sender, EventArgs e)
        {
            nRefresh();
        }

        private void mSystemInfo_Click(object sender, EventArgs e)
        {
            nInfo();
        }

    }
}
